/*子应用*/
const express = require('express');

const query = require('../pool');

const product = express.Router();

// // 蛋糕列表搜索
// product.get('/list',async(req,res)=>{
//   let obj = req.query;
// //console.log(obj)
//   let params = `%${obj.names}%`
//   let result =await query('select cakename from cakelist where cakename like ?',[params]);
// //console.log(result)
//   if(result.length==0){
//     res.send({code:0,msg:'未查询到相关结果'})
//   }else{
//     res.send({code:1,msg:'搜索成功',data:result})
//   }
// })
// 蛋糕的商品列表
product.get('/list?:page', async (req, res) => {
  let obj = req.query;
  let page = `${obj.page*9},8`;
  // console.log(obj);
  let result = [];
  // console.log(obj.typeid);
  let sql_params = ''
  // console.log(obj.typeid);
  if(obj.typeid!='others'){
    sql_params = `and taste_type = ${obj.typeid}`
  }
  if(obj.method=='1'){
    result = await query(`select * from cakelist left join taste_type on cakelist.taste_type=taste_type.typeid left join discount_type on cakelist.discount_type=discount_type.tid where cakelist.family="蛋糕" ${sql_params} order by cid asc limit ${page}`
  );
  }
  if(obj.method=='2'){
    result = await query(`select c.cid,c.cakename,sum(od.count),c.* from order_detail od left join cakelist c on od.product_id = c.cid  where c.family="蛋糕" ${sql_params}   group by c.cid order by sum(od.count) desc limit ${page}`)
  }
  
  if (result.length == 0) {
    res.send({ code: 0, msg: '未查询到相关结果' })
  } else {
    res.send({ code: 1, msg: '搜索成功', data: result })
  }
})
//蛋糕的口味列表
product.get('/taste', async (req, res) => {
  let result = await query(`select * from taste_type;`
  );
  if (result.length == 0) {
    res.send({ code: 0, msg: '未查询到相关结果' })
  } else {
    res.send({ code: 1, msg: '搜索成功', data: result })
  }
});
//根据蛋糕的口味来渲染页面
product.get('/flavor_rendering', async (req, res) => {
  let obj = req.query;
  let tastes = obj.typeid;
  //搜索到相关的口味对应
  let result = await query(`select * from cakelist left join taste_type on (cakelist.taste_type=taste_type.typeid) where cakelist.taste_type=${tastes}`
  );
  if (result.length == 0) {
    res.send({ code: 0, msg: '未查询到相关结果' })
  } else {
    res.send({ code: 1, msg: '搜索成功', data: result })
  }
});
// 蛋糕加购买
// product.get('/parts',async(req,res)=>{
//   let result =await query(`select * from cakelist left join taste_type on cakelist.taste_type=taste_type.typeid left join discount_type on cakelist.discount_type=discount_type.tid where cakelist.family="配件" order by cid asc;` 
//   );
// //console.log(result)
//   if(result.length==0){
//     res.send({code:0,msg:'未查询到相关结果'})
//   }else{
//     res.send({code:1,msg:'搜索成功',data:result})
//   }
// });

//暴露
module.exports = product;
